package com.example.tests;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.example.obj.GroupObject;
import com.example.obj.GroupSet;

public class Test_04_GroupRemoval extends TestBase{
	
	Random rnd = new Random();
		
	@BeforeMethod
	public void init(){
		if (manager.getGroupHelper().getGroupsCouche().size() == 0) {
			log.info("Before: Start group removal");
			log.info("There are no groups for removal. Try to create one");
			manager.getGroupHelper().createGroup(new GroupObject().setName("name"));
		}
	}
	
	@Test(priority = 4)
	public void testGroupCanBeDeleted() throws Exception {
		log.info("Start testGroupCanBeDeleted");
		GroupSet oldGroupSet = manager.getGroupHelper().getGroupsCouche();		
		assertThat(oldGroupSet.size() > 0, is(true));
		log.info("Old groups list: " + oldGroupSet);
		GroupObject group = oldGroupSet.getElement(rnd.nextInt(oldGroupSet.size()));
		manager.getGroupHelper().deleteGroup(group.id);
		GroupSet newGroupSet = manager.getGroupHelper().getGroupsCouche();
		log.info("New groups list: " + newGroupSet);
		log.info("Start assertions: size, lists' elements");
		assertThat(newGroupSet.size(), equalTo(oldGroupSet.size() - 1));
		assertThat(newGroupSet, equalTo(oldGroupSet.without(group)));
		log.info("Stop assertions");
	}
	
	@AfterMethod
	public void testUIConfirmWithDB(){
		log.info("After testGroupCanBeDeleted");
		GroupSet groupsUI = manager.getGroupHelper().getGroupsUI();
		log.info("Groups from UI: " + groupsUI);
		GroupSet groupsDB = manager.getGroupHelper().getGroupsCouche();
		log.info("Groups from DB: " + groupsDB);
		assertThat(groupsDB, equalTo(groupsUI));
	}
}
